package com.homework.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.homework.dto.ClassScheduleDTO;
import com.homework.dto.Result;
import com.homework.entity.ClassSchedule;
import com.homework.entity.Student;
import com.homework.mapper.ClassScheduleMapper;
import com.homework.mapper.StudentMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class ClassScheduleService {

    private final ClassScheduleMapper classScheduleMapper;
    private final StudentMapper studentMapper;

    /**
     * 获取学生的完整课程表
     */
    public Result<List<ClassSchedule>> getClassSchedule(Long studentId) {
        // 验证学生是否存在
        Student student = studentMapper.selectById(studentId);
        if (student == null) {
            return Result.error("学生不存在");
        }

        List<ClassSchedule> schedule = classScheduleMapper.selectByStudentId(studentId);
        return Result.success(schedule);
    }

    /**
     * 获取明天的课程
     */
    public Result<List<ClassSchedule>> getTomorrowClasses(Long studentId) {
        // 验证学生是否存在
        Student student = studentMapper.selectById(studentId);
        if (student == null) {
            return Result.error("学生不存在");
        }

        // 计算明天是周几
        LocalDate tomorrow = LocalDate.now().plusDays(1);
        DayOfWeek dayOfWeek = tomorrow.getDayOfWeek();

        // 转换为数据库存储的格式 (周一=1, 周日=7)
        int dayValue = dayOfWeek.getValue();

        // 周六和周日没有课程
        if (dayValue > 5) {
            return Result.success(new ArrayList<>());
        }

        List<ClassSchedule> tomorrowClasses = classScheduleMapper.selectByStudentIdAndDay(studentId, dayValue);
        return Result.success(tomorrowClasses);
    }

    /**
     * 更新课程表单项
     */
    @Transactional
    public Result<String> updateClassSchedule(Long id, ClassScheduleDTO dto) {
        ClassSchedule existingSchedule = classScheduleMapper.selectById(id);
        if (existingSchedule == null) {
            return Result.error("课程不存在");
        }

        // 更新课程信息
        ClassSchedule schedule = new ClassSchedule();
        schedule.setId(id);
        schedule.setSubject(dto.getSubject());
        schedule.setTeacherName(dto.getTeacherName());
        schedule.setClassroom(dto.getClassroom());
        schedule.setStartTime(dto.getStartTime());
        schedule.setEndTime(dto.getEndTime());
        schedule.setIsActive(dto.getIsActive());

        classScheduleMapper.updateById(schedule);
        return Result.success("课程更新成功");
    }

    /**
     * 批量更新课程表
     */
    @Transactional
    public Result<String> batchUpdateSchedule(Long studentId, List<ClassScheduleDTO> scheduleList) {
        // 验证学生是否存在
        Student student = studentMapper.selectById(studentId);
        if (student == null) {
            return Result.error("学生不存在");
        }

        // 删除原有课程表
        classScheduleMapper.delete(new LambdaQueryWrapper<ClassSchedule>()
            .eq(ClassSchedule::getStudentId, studentId));

        // 插入新的课程表
        for (ClassScheduleDTO dto : scheduleList) {
            ClassSchedule schedule = new ClassSchedule();
            schedule.setStudentId(studentId);
            schedule.setDayOfWeek(dto.getDayOfWeek());
            schedule.setSessionOrder(dto.getSessionOrder());
            schedule.setSubject(dto.getSubject());
            schedule.setTeacherName(dto.getTeacherName());
            schedule.setClassroom(dto.getClassroom());
            schedule.setStartTime(dto.getStartTime());
            schedule.setEndTime(dto.getEndTime());
            schedule.setIsActive(dto.getIsActive());

            classScheduleMapper.insert(schedule);
        }

        return Result.success("课程表更新成功");
    }

    /**
     * 删除课程
     */
    @Transactional
    public Result<String> deleteClassSchedule(Long id) {
        ClassSchedule schedule = classScheduleMapper.selectById(id);
        if (schedule == null) {
            return Result.error("课程不存在");
        }

        classScheduleMapper.deleteById(id);
        return Result.success("课程删除成功");
    }

    /**
     * 获取星期几的中文名称
     */
    public static String getDayName(int dayOfWeek) {
        switch (dayOfWeek) {
            case 1: return "周一";
            case 2: return "周二";
            case 3: return "周三";
            case 4: return "周四";
            case 5: return "周五";
            default: return "未知";
        }
    }

    /**
     * 获取节次的中文名称
     */
    public static String getSessionName(int sessionOrder) {
        switch (sessionOrder) {
            case 1: return "第一节";
            case 2: return "第二节";
            case 3: return "第三节";
            case 4: return "第四节";
            case 5: return "第五节";
            case 6: return "第六节";
            case 7: return "第七节";
            case 8: return "第八节";
            default: return "未知";
        }
    }
}